<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 分割线 -->
        <el-divider />
        <!-- 内容 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <tree-tools slot-scope="{ data }" :tree-node="data" @editDepts="editDepts" @addDepts="addDepts" @delDepts="getDepartments" />
        </el-tree>
      </el-card>
      <!-- 新增部门的弹层 -->
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      loading: false, // 设置loading效果
      company: {}, // 公司头部信息
      departs: [], // 员工组织架构信息
      defaultProps: {
        label: 'name'
      },
      showDialog: false, // 显示不显示新增弹窗层
      node: null // 记录点击的当前部门
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取组织架构列表
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeData(result.depts, '')
      this.loading = false
    },
    // 子组件传递的显示新增弹层
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    // 点击编辑弹层
    editDepts(node) {
      // 首先打开弹层
      this.showDialog = true
      this.node = node // 赋值操作的节点
      // 我们需要在这个位置 调用子组件的方法
      // 父组件 调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
